import {
	getParm
} from './stringUtil.js'

function _handlerSence() {
	var result = {ChannelType:0,ChannelAppId:"",ChannelCode:""};
	var pages = getCurrentPages();
	// #ifdef MP-WEIXIN
	var page_options = pages[pages.length - 1].options;
	var cid = getParm(page_options, "cid");
	if (cid) {
		result.ChannelType = 1;
		result.ChannelAppId = cid;
	} else {
		var options = wx.getLaunchOptionsSync();
		switch (options.scene) {
			//公众号自定义菜单
			case 1035:
				result.ChannelType = 1;
				result.ChannelAppId = options.referrerInfo.appId;
				break;
				//小程序跳小程序
			case 1037:
				result.ChannelType = 4;
				result.ChannelAppId = options.referrerInfo.appId;
				break;
				//公众号模板消息
			case 1043:
				result.ChannelType = 8;
				result.ChannelAppId = options.referrerInfo.appId;
				break;
				//扫码
			case 1011:
			case 1012:
			case 1013:
			case 1047:
			case 1048:
			case 1049:
				result.ChannelType = 2;
				result.ChannelCode = decodeURIComponent(options.query.scene);
				break;
		}
	}
	// #endif
	// #ifdef H5
	var cid = getParm(window.location.href, "cid");
	if (cid) {
		result.ChannelType = 1;
		result.ChannelAppId = cid;
	}
	var scene = getParm(window.location.href, "scene");
	if (scene) {
		result.ChannelType = 2;
		result.ChannelCode = scene;
	}
	// #endif
	return result;
}

export const handlerSence = _handlerSence;
